package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.admin.model.AdminMerchantsInfo;
import cn.com.wangluotongxin.common.pojo.LoginUser;
import cn.com.wangluotongxin.common.pojo.TenantUser;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.merchants.form.LoginForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsUserService;
import cn.com.wangluotongxin.merchants.vo.LoginTokenVo;
import cn.com.wangluotongxin.merchants.vo.UserVo;
import cn.com.wangluotongxin.web.utils.AuthUtil;
import cn.com.wangluotongxin.web.utils.TenantAuthUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Api(tags = "网络通信- 登录相关接口")
@RestController
@RequestMapping("/")
public class IndexController {

    @Autowired
    private IMerchantsUserService userService;

    @ApiOperation("账号登录")
    @PostMapping("/login/pwd")
    public AjaxResult<LoginTokenVo> loginPwd(@RequestBody LoginForm form){
        Assert.isTrue(StrUtil.isNotBlank(form.getMerchantsNo()), "缺少商户号");
        Assert.isTrue(StrUtil.isNotBlank(form.getUsername()), "缺少账号");
        Assert.isTrue(StrUtil.isNotBlank(form.getPassword()), "缺少密码");

        String token = userService.getToken(form.getUsername(), form.getPassword(), form.getMerchantsNo());
        return AjaxResult.success(new LoginTokenVo(token));
    }

    @ApiOperation("用户信息")
    @GetMapping("/info")
    public AjaxResult<UserVo> info(){
        TenantUser tenantUser = TenantAuthUtil.getTenantUser();
        return AjaxResult.success(userService.getInfo(tenantUser.getId()));
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public AjaxResult logout(){
        return AjaxResult.success();
    }
}
